Mandatory guidelines are of the kind that are used by programming professionals. Recommended guidelines are of the kind that are appropriate for beginning programmers (because they improve understanding) but that are less important for professionals.
The name of the file and the name of the class must coincide
exactly. For example the Queue
class should be
in a file named Queue.java
.
Note that Java is case-sensitive (even though some versions of
MS-Windows are not).
The only exception to this rule is that all constructors must
be listed first. For example, in a Queue
class, the
constructor(s) should be first, the pop
method should
be second, and the push
method should be third.
In other words, class/instance variables should be declared at the top of the class (before any methods are declared) and variables that are local to a method should be declared at the top of that method (before any other lines of code). The only exception to this rule is that index variables in loops may be declared locally.
So, the following is discouraged:
int age = 35;
and you should, instead, do the following:
int age;
age = 35;
Within each category, public variables must be declared first, followed by protected variables, followed by package variables, followed by private variables.
For example, variables of type double
should be declared
before variables of type int
which should, in turn,
be declared before variables of type Node
.
In addition, each "word" within a class name should start
with an uppercase letter.
For example, TextMessage
and
SimpleTrafficMonitor
are both appropriate class names.
final
variables must be in
all uppercase.
Further, "words" within a "constant" name must be delimited by
an underscore character.
For example, EXTREMELY_UNHEALTHY
is an
appropriate name for a "constant".
Further, each "word" within a variable name should start
with an uppercase letter.
For example, importantMessage
and
campusMonitor
are both appropriate variable names.
In general, even single-character variable names should be lowercase. However, in some situations, mathematical notation uses uppercase letters. In such situations, uppercase variable names may be used. For example, matrices are often written using uppercase letters. So, an expression like (b = A*x) would be appropriate.
Variable names like aaa
are not appropriate.
Index variables and counters can, however, have names like
i
and j
.
Do not rely on default visibilities.
public
/private
, static
,
final
.
Indeed, they must have private visibility unless there is a very good reason for them not to.
This comment should describe the complete class (rather than
the methods in the class) and must include: an @author
(containing your name), a @version
element
(containing the due date), and the line
This work complies with the MU Honor Code.
.
This comment should describe the methods, its parameters, and its return value.
@author
and @version
elements. The comment describing a
method must have an @param
element for each
formal parameter and must have a @return
element
if the method is not void
. These comments
must be in the order they are listed and must not be empty.
javadoc
is a program (written in Java) that creates external documentation
(in HTML) from block comments. All of the Java documentation was,
in fact, created this way. javadoc block comments start with
/**
and end with */
.
//
rather than /* ... */
(because it makes it
easier to "comment out" a section of code while
you are debugging).
//
.
Except for array initializations, which must be indented 8 spaces. Also, tabs characters must not be used.
{
must appear at the end of
the line containing conditional and loop structures.
{
must appear at the end of
the line containing conditional and loop structures.
}
must appear at the start of
a new line (because it makes it easy to identify the
end of a block). The else {
, if there is one,
must appear on the same line (after a space).
{
and }
.
this.
(so that you can
easily distinguish t hem from local variables).
switch
statements should have a
default
clause.
default
clause should be after
all case
clauses in a switch
statement.
case
clauses in switch
statements
should not fall-through
return
statements.
{}
)
must only be used with if
statements and loops.
Parameters in constructors and setters may.
==
or !=
operators.
*
wildcard must not be used
in import
statements (because it makes
it difficult to see dependencies).
They may be used with JUnit packages.
for
loop control variables must not
be changed within the body of the loop.
long
literals must use an uppercase
L
(because the lowercase l
is difficult to distinguish from a 1
).
They may be used with JUnit packages.
Copyright 2020